

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class TailDialog extends JDialog implements ComponentListener, WindowListener
                        {

	private JFrame sourceFrame;
	private AnimatingPlayer player;
	private Component lastGlass;
	private JPanel modalGlass;
	
	public TailDialog(JFrame sourceFrame){
		super();
		this.sourceFrame = sourceFrame;
		this.setUndecorated(true);
		setAccordingLocation();
		this.sourceFrame.addComponentListener(this);
		this.sourceFrame.addWindowListener(this);
		
		lastGlass = sourceFrame.getGlassPane();
		modalGlass = new JPanel();
		modalGlass.setOpaque(false);
		modalGlass.addMouseListener(new MouseAdapter(){});
		modalGlass.addMouseMotionListener(new MouseMotionAdapter(){});
	}
	
	public Point getAccordingLocation(){
		Component sourceComp = this.sourceFrame.getLayeredPane();
		Point p = sourceComp.getLocationOnScreen();
		Dimension size = sourceComp.getSize();
		
		Point p1 = new Point(p.x + size.width + 5, p.y);
		return p1;
	}
	
	private void setAccordingLocation(){
		
		setLocation(getAccordingLocation());
	}

	public void componentHidden(ComponentEvent e) {
	}

	public void componentMoved(ComponentEvent e) {
		setAccordingLocation();
	}

	public void componentResized(ComponentEvent e) {
		setAccordingLocation();
	}

	public void componentShown(ComponentEvent e) {
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		this.setVisible(true);
	}

	public void windowIconified(WindowEvent e) {
		this.setVisible(false);
	}

	public void windowOpened(WindowEvent e) {
	}
	
	public void showDialog(){
		player = new AnimatingPlayer(this);
		player.setAnimationDirection(AnimatingPlayer.INCOMING);
		player.playAnimation();
	}
	
	public void closeDialog(){
		this.dispose();
		this.sourceFrame.removeWindowListener(this);
		this.sourceFrame.removeComponentListener(this);
		if(player != null){
			player.setAnimationDirection(AnimatingPlayer.OUTCOMING);
			player.playAnimation();
		}
	}
	
	public void doBlock(){
	   this.sourceFrame.setGlassPane(modalGlass);
	   modalGlass.setVisible(true);
	}
	
	public void cancelBlock(){
		modalGlass.setVisible(false);
		this.sourceFrame.setGlassPane(lastGlass);
	}
}
